<h1><?php echo $fund['name']; ?></h1>
<ul id="breadcrumb">
	<li><span>รายงานการรับ-จ่ายเงิน</span></li>		
	<li><span><?php echo 'ปีงบประมาณ '.$year; ?></span></li>		
	<li><?php echo 'เดือน '.month($month); ?></li>	
</ul>
<div id="content">
	<div class="search">
		<form aaction="income_expenses/view/<?php echo $fund['id']; ?>" method="get">
			<?php if(level_data('view')): ?>
			<label>หน่วยงาน</label> <?php echo form_dropdown('agency_id', get_option('id', 'name', 'agencies'), $agency_id); ?>&nbsp;&nbsp;
			<?php else: echo form_hidden('agency_id', login_data('agency_id')); ?>
			<?php endif; ?>
			&nbsp;&nbsp;<label>ปีงบประมาณ</label> <?php echo form_dropdown('year', get_year_option('2550'), $year); ?>
			&nbsp;&nbsp;<label>เดือน</label> <?php echo form_dropdown('month', get_month(), $month); ?>
			<input type="submit" value="<?php echo lang('search'); ?>" class="button" />
		</form>
	</div>
	<div class="head">
		<p><?php echo $fund['name'] ?></p>
		<p>รายงานการรับ-จ่ายเงิน</p>
		<p>ประจำปีงบประมาณ   <?php echo $year ?> เดือน <?php echo month($month); ?></p>
	</div>
	<table class="list">
		<tr>
			<th></th>
			<th width="50%"></th>
			<th width="250"></th>
			<th width="150">เดือนนี้</th>
			<th width="150">รวมต้นปี</th>
		</tr>
		
		<?php 
			$side = 0; 
			$sub = 0; 
			$col_plus = NULL; 
			$total_month_price = 0; 
			$total_year_price = 0;
			$all_total_month_price = 0; 
			$all_total_year_price = 0; 
		?>
		<?php foreach($result as $key => $item): ?>
		
		<?php 
			if(in_array($item['col'], $haystack)): 
			
				if(in_array($item['col'], array(12,17,32,37,47)))
				{ 
					$col_plus = '&nbsp;จำนวน '.number_format($item['total_price']).' ราย'; 
				}
				if(in_array($item['col'], array(14,19,34,39,49)))
				{
					$col_plus2 = '&nbsp;จำนวน '.number_format($item['total_price']).' โครงการ';
				} 
				if(in_array($item['col'], array(15,20,35,40,50)))
				{
					$col_plus = $col_plus2.'&nbsp;'.number_format($item['total_price']).' องค์กร';	
				} 
				
		?>
		
		<?php else: ?>
		
		<?php if($item['side'] > $side or $item['sub'] > $sub):?>	
		
		<tr>
			<td><?php echo ($item['side'] > $side) ? '<b>'.$side_label[$item['side']].'</b>' : NULL; ?></td>
			<td><?php echo ($item['sub'] > $sub or ($item['side'] > $side)) ? '<b>'.$sub_label[$item['side']][$item['sub']].'</b>' : NULL ?></td>
			<td></td>
			<td></td>
			<td></td>
		</tr>
		<?php $side = $item['side']; $sub = $item['sub']; ?> 
		
		<?php endif; ?>
		
		<?php 
		$month_price = (!empty($item['total_price'])) ? $item['total_price'] : NULL; 
		$total_month_price += $month_price;
		
		$year_price = ($item['id'] == 43) ? $first_budget : ((!empty($item['year_price'])) ? $item['year_price'] : NULL);

		$total_year_price += $year_price;
		?>
		
		<tr>
			<td>
				<?php if($item['side'] > $side){
					$side = $item['side'];
					echo $side_data[$side];  
				}; ?>
			</td>
			<td><?php echo ($item['id'] == 43) ? '<b>'.$item['col_title'].'</b>' : $item['col_title']; ?></td>
			<td><?php echo $col_plus; $col_plus = NULL; ?></td>
			<td style="text-align:right"><?php echo ($month_price) ? number_format($month_price, 2) : NULL; ?></td>
			<td style="text-align:right"><?php echo ($year_price) ? number_format($year_price, 2) : NULL; ?></td>
		</tr>
		
		<?php if($item['id'] == 21): ?>
		<tr>
			<td></td>
			<td><?php echo '<b>'.$sub_label[1][4].'</b>'; ?></td>
			<td></td>
			<td style="text-align:right"><?php echo number_format($total_month_price, 2); $all_total_month_price = $total_month_price; $total_month_price = 0; ?></td>
			<td style="text-align:right"><?php echo number_format($total_year_price, 2); $all_total_year_price = $total_year_price; $total_year_price = 0; ?></td>
		</tr>
		<tr><td>&nbsp;</td><td></td><td></td><td></td><td></td></tr>
		<?php endif; ?>	
		
		<?php if($item['id'] == 42): ?>
		<tr>
			<td></td>
			<td><?php echo '<b>'.$sub_label[2][3].'</b>'; ?></td>
			<td></td>
			<td style="text-align:right"><?php echo number_format($total_month_price, 2); ?></td>
			<td style="text-align:right"><?php echo number_format($total_year_price, 2); ?></td>
		</tr>
		<tr>
			<td></td>
			<td><b>ยอดยกไป</b></td>
			<td></td>
			<td style="text-align:right"><?php echo number_format($all_total_month_price - $total_month_price, 2); ?></td>
			<td style="text-align:right"><?php echo number_format($all_total_year_price - $total_year_price, 2); ?></td>
		</tr>
		<?php endif; ?>	
		
		<?php endif; ?>
		
		<?php endforeach; ?>
	</table>
</div>